import { createSlice } from "@reduxjs/toolkit";
import axios from 'axios'
const channelStore = createSlice({
  name: 'channel',
  initialState: {
    channelList: []
  },
  reducers: {
    setChannels (state, action) {
      state.channelList = action.payload
      console.log('action.payload', action.payload)
      console.log(state.channelList)
    }
  }
})


// 异步请求

const { setChannels } = channelStore.actions

const fetchChannelList = () => {
  return async (dispatch) => {
    const res = await axios.get('http://geek.itheima.net/v1_0/channels')
    console.log('channels', res.data.data.channels)
    dispatch(setChannels(res.data.data.channels))
  }
}

export { fetchChannelList }

const reducer = channelStore.reducer

export default reducer


